﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfPaint
{
    /// <summary>
    /// Lógica de interacción para Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Paint paint = new Paint();
        Shape myShape = null;

        Microsoft.Win32.OpenFileDialog ofd;
        Microsoft.Win32.SaveFileDialog sfd;

        public Window1()
        {
            InitializeComponent();
            ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.FileOk += new System.ComponentModel.CancelEventHandler(ofd_FileOk);

            sfd = new Microsoft.Win32.SaveFileDialog();
            sfd.FileOk += new System.ComponentModel.CancelEventHandler(sfd_FileOk);
        }

        private void Label_MouseDown(object sender, MouseButtonEventArgs e)
        {
            paint.figuraActual = Paint.FiguraAPintar.Cercle;
        }

        private void myCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //Dibuixar una elipse.
            if (myShape == null)
            {
                Point p = e.GetPosition(this.myCanvas);
                paint.ix = (int)p.X;
                paint.iy = (int)p.Y;

                if (paint.figuraActual == Paint.FiguraAPintar.Cercle)
                {
                    Ellipse el = new Ellipse();
                    el.Width = 1;
                    el.Height = 1;
                    Canvas.SetLeft(el, paint.ix);
                    Canvas.SetTop(el, paint.iy);
                    el.Stroke = Brushes.Black;
                    el.StrokeThickness = 3;
                    el.Tag = Paint.FiguraAPintar.Cercle;
                    myShape = el;
                }
                if (paint.figuraActual == Paint.FiguraAPintar.Rectangle)
                {
                    Rectangle r = new Rectangle();
                    r.Width = 1;
                    r.Height = 1;
                    Canvas.SetLeft(r, paint.ix);
                    Canvas.SetTop(r, paint.iy);
                    r.Stroke = Brushes.Red;
                    r.StrokeThickness = 3;
                    r.Tag = Paint.FiguraAPintar.Rectangle;
                    myShape = r;
                }
                if (paint.figuraActual == Paint.FiguraAPintar.Linia)
                {
                    Line l = new Line();
                    l.X2 = 1;
                    l.Y2 = 1;
                    l.X1 = paint.ix;
                    l.Y1 = paint.iy;
                    l.Stroke = Brushes.Red;
                    l.StrokeThickness = 3;
                    l.Tag = Paint.FiguraAPintar.Linia;
                    myShape = l;
                }
                myCanvas.Children.Add(myShape);
            }
        }

        private void myCanvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            myShape = null;
        }

        private void myCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (myShape != null)
            {
                Point p = e.GetPosition(this.myCanvas);
                paint.fx = (int)p.X;
                paint.fy = (int)p.Y;

                //Modificar el tamany, això sempre serà igual.
                myShape.Width = Math.Abs(paint.fx - paint.ix);
                myShape.Height = Math.Abs(paint.fy - paint.iy);

                if ((paint.ix < paint.fx) &&
                (paint.iy < paint.fy))
                {
                   //No fer res estem en 4art quadrant.
                    ;
                }
                if ((paint.ix > paint.fx) &&
                (paint.iy > paint.fy))
                {
                    //2on quadrant.
                    //Intercanviem punts.
                    Canvas.SetTop(myShape, paint.fy);
                    Canvas.SetLeft(myShape, paint.fx);
                }
                if ((paint.ix > paint.fx) &&
                (paint.iy < paint.fy))
                {
                    //3er quadrant.
                    Canvas.SetLeft(myShape, paint.fx);
                    Canvas.SetTop(myShape, paint.iy);
                }

                if ((paint.ix < paint.fx) &&
                (paint.iy > paint.fy))
                {
                    //1er quadrant.
                    Canvas.SetLeft(myShape, paint.ix);
                    Canvas.SetTop(myShape, paint.fy);
                }

            }
        }

        private void Label_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            this.paint.figuraActual = Paint.FiguraAPintar.Rectangle;
        }

        private void Label_MouseDown_2(object sender, MouseButtonEventArgs e)
        {
            this.myCanvas.Children.Clear();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Label_MouseDown_3(object sender, MouseButtonEventArgs e)
        {
            sfd.ShowDialog();
        }

        void sfd_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            System.Xml.XmlTextWriter tw = 
                new System.Xml.XmlTextWriter(sfd.FileName, Encoding.UTF8);

            tw.Formatting = System.Xml.Formatting.Indented;
            tw.WriteStartDocument();
           
            //Codi obrir el fitxer.
            tw.WriteStartElement("paint");
            for (int i = 0; i < myCanvas.Children.Count; i++)
            {
                Shape s = (Shape)myCanvas.Children[i];
                
                tw.WriteStartElement(s.Tag.ToString());
                tw.WriteAttributeString("left", Canvas.GetLeft(s).ToString());
                tw.WriteAttributeString("top", Canvas.GetTop(s).ToString());
                tw.WriteAttributeString("width", s.Width.ToString());
                tw.WriteAttributeString("height", s.Height.ToString());
                tw.WriteEndElement();


                Console.WriteLine("Tipus: {2}, left: {3}, top: {4}, Width: {0}, Height:{1}", 
                    s.Width, s.Height, s.Tag,
                    Canvas.GetLeft(s).ToString(), Canvas.GetTop(s).ToString());
            }
            tw.WriteEndElement();
            tw.WriteEndDocument();
            tw.Flush();
            tw.Close();
        }

        void ofd_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            String prova = ofd.FileName;
            //Codi per guardar el fitxer.


        }

        private void Label_MouseDown_4(object sender, MouseButtonEventArgs e)
        {
            paint.figuraActual = Paint.FiguraAPintar.Linia;
        }
    }
}
